package ch.pizzapp.kurier;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import ch.pizzapp.kurier.adapter.GenerateGridAdapter;
import ch.pizzapp.kurier.adapter.GridNormalAdapater;
import ch.pizzapp.kurier.client.InfoClient;
import ch.pizzapp.kurier.data.CategoryData;
import ch.pizzapp.kurier.data.GenarateItem;
import ch.pizzapp.kurier.data.GenarateItemResponceData;
import ch.pizzapp.kurier.data.Size;
import ch.pizzapp.kurier.dto.InfoDto;
import ch.pizzapp.kurier.view.GeneratorItem;
import ch.pizzapp.kurier.view.SelectCallCallBack;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass. Use the
 * {@link PizzaGenaratorFragment#newInstance} factory method to create an
 * instance of this fragment.
 * 
 */
@EFragment(R.layout.fragment_pizza)
public class PizzaGenaratorFragment extends BaseFragmnet {

	// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
	private static final String CATEGORY_DATA = "category";

	@ViewById(R.id.generatorItem1)
	GeneratorItem generatorItem;

	@ViewById(R.id.priceVale)
	TextView priceVale;

	@ViewById(R.id.buttonSelected)
	Button buttonSelected;

	Double bunSizePrice = 0.0;

	@Bean
	GenerateGridAdapter generateGridAdapter;

	private SelectCallCallBack mCallback;

	public void setmCallback(SelectCallCallBack callback) {
		this.mCallback = callback;
	}

	@Bean
	InfoDto infoDto;

	@RestService
	InfoClient infoClient;

	// private String mParam1;
	// private String mParam2;
	@FragmentArg(CATEGORY_DATA)
	CategoryData categoryData;

	private GridNormalAdapater gridNormalAdapater;

	// @ViewById(R.id.text)
	// TextView text;

	public static PizzaGenaratorFragment newInstance(CategoryData categoryData) {
		PizzaGenaratorFragment fragment = new PizzaGenaratorFragment_();
		Bundle args = new Bundle();
		args.putParcelable(CATEGORY_DATA, categoryData);
		fragment.setArguments(args);
		return fragment;
	}

	@Click({ R.id.buttonSelected })
	void onClick(View vuew) {
		switch (vuew.getId()) {
		case R.id.buttonSelected:

			if (gridNormalAdapater != null) {

				ArrayList<GenarateItem> selectedItems = gridNormalAdapater
						.getCheckedItems();

				if (selectedItems.size() > 0) {

					mCallback.onClicked(selectedItems, sizeSend);
				} else {
					showMessge("please select at leat one item");
				}
			}
			break;

		default:
			break;
		}
	}

	Size sizeSend;

	@AfterViews
	void afterView() {
		// text.setText("name" + categoryData.getTitle());

		generatorItem.setOnCallBack(new GeneratorItem.Callback() {

			@Override
			public void onClicked(Size size) {
				// TODO Auto-generated method stub
				priceVale.setText("" + size.getPrice());

				bunSizePrice = Double.parseDouble(size.getPrice());

				sizeSend = size;
			}

			// @Override
			// public void onClicked(String value) {
			//
			// priceVale.setText("" + value);
			// }

		});
		getItems();

	}

	//Map<String, Integer> datesMap = new HashMap<String, Integer>();

	@Background
	void getItems() {

//		datesMap.put("Mon", 1);
//		datesMap.put("Tue", 2);
//		datesMap.put("Wed", 3);
//		datesMap.put("Thu", 4);
//		datesMap.put("Fri", 5);
//		datesMap.put("Sat", 6);
//		datesMap.put("Sun", 7);

		//SimpleDateFormat dateFormat = new SimpleDateFormat("EEE");
		Date date = new Date();

		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
		// Date date = new Date();

		infoDto.setDate(sm.format(date));
		Calendar calendar = Calendar.getInstance();
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1)
        {
            infoDto.setDay("7");
        } else
        {
            infoDto.setDay(String.valueOf(-1 + calendar.get(Calendar.DAY_OF_WEEK)));
        }
		infoDto.setType("9");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {

					setGenarate(infoClient.getGenarate(infoDto
							.getMultiValuedMap()));

				} catch (Exception e) {
					showProgress(false);
					System.out.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {
				showProgress(false);
				showMessge("Not connected ");
			}
		});
	}

	@UiThread
	void setGenarate(GenarateItemResponceData genarateItemResponceData) {

		showProgress(false);

		if (genarateItemResponceData != null) {
			if (genarateItemResponceData.getResponse().getSizes() != null) {

				if (genarateItemResponceData.getResponse().getSizes().size() > 0) {

					priceVale.setText(genarateItemResponceData.getResponse()
							.getSizes().get(0).getPrice());

					bunSizePrice = Double.parseDouble(genarateItemResponceData
							.getResponse().getSizes().get(0).getPrice());

					sizeSend = genarateItemResponceData.getResponse()
							.getSizes().get(0);
					generatorItem.setSizes(genarateItemResponceData
							.getResponse().getSizes());
				}
			}
			if (genarateItemResponceData.getResponse().getItems() != null) {
				gridNormalAdapater = new GridNormalAdapater(getActivity(),
						genarateItemResponceData.getResponse().getItems());
			}

			gridView.setAdapter(gridNormalAdapater);
		}

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	public PizzaGenaratorFragment() {
		// Required empty public constructor
	}

}
